package com.hemerasolutions.v2trainer.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.hemerasolutions.v2trainer.dao.interfaces.ExerciseGroupDaoInterface;
import com.hemerasolutions.v2trainer.model.exerciseGroup.ExerciseGroup;
import com.hemerasolutions.v2trainer.model.exerciseGroup.ExerciseGroup_;
import com.hemerasolutions.v2trainer.model.exerciseGroup.VersionableExerciseGroup;
import com.hemerasolutions.v2trainer.model.exerciseGroup.VersionableExerciseGroup_;
import com.hemerasolutions.v2trainer.model.library.Library;
import com.hemerasolutions.v2trainer.model.modality.Modality;
import com.hemerasolutions.v2trainer.model.modality.Modality_;
import com.hemerasolutions.v2trainer.model.training.TrainingType;

@Repository(value = "exeriseGroupDao")
public class ExerciseGroupDao extends BaseDao<ExerciseGroup> implements ExerciseGroupDaoInterface {

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	@Override
	public List<ExerciseGroup> searchExerciseGroups(Map<String, Object> fields) {
		List<Predicate> predicateList = new ArrayList<Predicate>();

		CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder();

		CriteriaQuery<ExerciseGroup> cq  = cb.createQuery(ExerciseGroup.class);
		
		Root<ExerciseGroup> exerciseGroup = cq.from(ExerciseGroup.class);

		Join<ExerciseGroup,Modality> joinModality = null;
		
		Predicate pred;
		
	    for (Map.Entry<String,Object> line : fields.entrySet()) {
	    	
	    	if( line.getKey().toString() == "name"){
	    		pred = cb.like(cb.upper(exerciseGroup.get(ExerciseGroup_.name)), "%"+line.getValue().toString().toUpperCase()+"%");
	    		predicateList.add(pred);    
	    		
	    	}else if (line.getKey().toString() == "description"){
	    		pred = cb.like(cb.upper(exerciseGroup.get(ExerciseGroup_.description)), "%"+line.getValue().toString().toUpperCase()+"%");
	    		predicateList.add(pred);    
	    		
	    	}else if (line.getKey().toString() == "obs"){
	    		pred = cb.like(cb.upper(exerciseGroup.get(ExerciseGroup_.obs)), "%"+line.getValue().toString().toUpperCase()+"%");
	    		predicateList.add(pred);    

	    	}else if( line.getKey().toString() == "begChangedAt" ){ 
	    		pred = cb.greaterThanOrEqualTo(exerciseGroup.get(ExerciseGroup_.lastChange), this.begDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		

	    	}else if( line.getKey().toString() == "endChangedAt" ){ 
	    		pred = cb.lessThanOrEqualTo(exerciseGroup.get(ExerciseGroup_.lastChange), this.endDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	
	    	}else if( line.getKey().toString() == "begCreatedAt" ){ 
	    		pred = cb.greaterThanOrEqualTo(exerciseGroup.get(ExerciseGroup_.createdAt), this.begDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	
	    	}else if( line.getKey().toString() == "endCreatedAt" ){ 
	    		pred = cb.lessThanOrEqualTo(exerciseGroup.get(ExerciseGroup_.createdAt), this.endDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		

	    	}else if( line.getKey().toString() == "modalitys" ){
	    		List<Modality> mods =  (List<Modality>)line.getValue();

	    		Expression<Modality> mod = exerciseGroup.get(ExerciseGroup_.modality);
	    		pred = mod.in(mods);
                predicateList.add(pred);    
	    	
	    	}else if( line.getKey().toString() == "trainingTypes" ){
	    		if(joinModality == null){
	    			joinModality = exerciseGroup.join(ExerciseGroup_.modality,JoinType.LEFT);
	    		}
	    			    		
	    		List<TrainingType> tts =  (List<TrainingType>)line.getValue();
	    		Expression<TrainingType> tt = joinModality.get(Modality_.trainingType);
	    		pred = tt.in(tts);
                predicateList.add(pred);    
    		
	    	}else if( line.getKey().toString() == "librarys" ){
	    		Path<VersionableExerciseGroup> veg = exerciseGroup.get(ExerciseGroup_.versionableExerciseGroup);
	    		Expression<Library> lib =  veg.get(VersionableExerciseGroup_.library);
	    		
	    		pred = lib.in((List<Library>)line.getValue());
	    		predicateList.add(pred);    		

	    	}else if( line.getKey().toString() == "active"){ 
	    		pred = cb.equal(exerciseGroup.get(ExerciseGroup_.active), (Boolean)line.getValue());
	    		predicateList.add(pred);    		

	    	}	    	
	    }
	    
	    Predicate[] predicates = new Predicate[predicateList.size()];
	    predicateList.toArray(predicates);
	    cq.where(predicates);
	 
	    
	    return super.getEntityManager().createQuery(cq).getResultList();		
	}

}
